package com.itheima.consumer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
    //条件注解：当某个属性值条件成立时，当前的配置文件才生效
@ConditionalOnProperty(name = "spring.rabbitmq.listener.simple.retry.enabled",havingValue = "true")
public class ErrorMessageConfig {
    //声明错误消息交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("error.direct");
    }

    //声明错误消息队列
    @Bean
    public Queue errorQueue(){
        return new Queue("error.queue",true);
    }

    //声明错误消息绑定上述的交换机和队列
    @Bean
    public Binding errorBinding(DirectExchange directExchange,Queue errorQueue){
        return BindingBuilder.bind(errorQueue).to(directExchange).with("error");
    }

    //声明失败消息处理
    @Bean
    public MessageRecoverer  messageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate,"error.direct","error");
    }
}
